//Usage:
// {
//   "personid" : "",
//   "groupid" : ""
// }
// 云函数入口文件

const cloud = require('wx-server-sdk')
const tencentcloud = require("tencentcloud-sdk-nodejs");

cloud.init()

var copyPerson = function (event) {
  console.log(event)
  console.log(event.personid)
  console.log(event.groupid)
  const IaiClient = tencentcloud.iai.v20180301.Client;
  const models = tencentcloud.iai.v20180301.Models;

  const Credential = tencentcloud.common.Credential;
  const ClientProfile = tencentcloud.common.ClientProfile;
  const HttpProfile = tencentcloud.common.HttpProfile;

  let cred = new Credential("AKIDa3PWXQYgBH6lGdoMo9mIratAJNLEtuX1", "ZM0ZZedXO7Vs1iQ2d8biJ1JbpHdtaCQH");
  let httpProfile = new HttpProfile();
  httpProfile.endpoint = "iai.tencentcloudapi.com";
  let clientProfile = new ClientProfile();
  clientProfile.httpProfile = httpProfile;
  let client = new IaiClient(cred, "ap-guangzhou", clientProfile);

  let req = new models.CopyPersonRequest();

  let params = `{"PersonId":"${event.personid}","GroupIds":["${event.groupid}"]}`
  req.from_json_string(params);

  return new Promise((resolve, reject) =>{
      client.CopyPerson(req, function (errMsg, response) {
      if (errMsg) {
        reject(errMsg)
      } else {
        resolve(response)
      }
    })
  })
}

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()

  var ret = await copyPerson(event)
  return ret
}